home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1995 March / Macworld CD-ROM (March 1995).cdr / Updaters / AppMaker 1.5.2->1.5.4 / Libraries / THINK / AMLibraryC / Dragging.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-25  |  6.9 KB  |  328 lines  |  [TEXT/KAHL]

  1. /* © 1991, Bowers Development Corp. */
  2. /* Dragging.c */
  3.  
  4. #include <QuickDraw.h>
  5. #include <ToolUtils.h>
  6. #include <TextEdit.h>
  7. #include <Windows.h>
  8.  
  9. #include "Globals.h"
  10. #include "ResourceDefs.h"    
  11.  
  12. #include "Dragging.h"
  13.  
  14. #pragma segment Dragging
  15.  
  16. /*----------*/
  17. static short Abs (short x);
  18. static short Abs (short x)
  19. {
  20.     return ((x < 0) ? (-x) : (x));
  21. } /*Abs*/
  22.  
  23. /*----------*/
  24. Boolean StartMove  (Point        *startPt, 
  25.                     short        *constraint)
  26. {
  27.     #define    slop    2
  28.  
  29.     Rect            slopRect;
  30.     Point            mousePt;
  31.  
  32.     #define sP        (*startPt)
  33.     SetRect (&slopRect, sP.h, sP.v, sP.h + 1, sP.v + 1);
  34.     #undef sP
  35.  
  36.     InsetRect (&slopRect, -slop, -slop);
  37.  
  38.     mousePt = *startPt;
  39.     while (StillDown () && PtInRect (mousePt, &slopRect)) {
  40.         GetMouse (&mousePt);
  41.     }
  42.  
  43.     if (StillDown ()) {
  44.         *constraint = noConstraint;
  45.         if ((curEvent.modifiers & optionKey) != 0) {
  46.             if (Abs (mousePt.h - (*startPt).h)
  47.             >  Abs (mousePt.v - (*startPt).v)) {
  48.                 *constraint = hAxisOnly;
  49.             } else {
  50.                 *constraint = vAxisOnly;
  51.             }
  52.         }
  53.     }
  54.     
  55.     *startPt = mousePt;
  56.     if ((*startPt).h > (slopRect).right)  { (*startPt).h = (slopRect).right;    }
  57.     if ((*startPt).h < (slopRect).left)   { (*startPt).h = (slopRect).left - 1;    }
  58.     if ((*startPt).v > (slopRect).bottom) { (*startPt).v = (slopRect).bottom;    }
  59.     if ((*startPt).v < (slopRect).top)    { (*startPt).v = (slopRect).top - 1;    }
  60.     
  61.     return (StillDown ());
  62. } /*StartMove*/
  63.  
  64. /*----------*/
  65. Boolean TrackMove  (RgnHandle            dragRgn,
  66.                     Point                startPt,
  67.                     Rect                bounds,
  68.                     short                constraint,
  69.                     short                *deltaH,
  70.                     short                *deltaV,
  71.                     DragGrayRgnProcPtr    actionProc)
  72. {
  73.     #undef slop
  74.     #define slop    20
  75.     
  76.     Rect            limitRect;
  77.     Rect            slopRect;
  78.     long            delta;
  79.     register Rect        *dragBBox;
  80.     
  81.     dragBBox = &(**dragRgn).rgnBBox;
  82.     SetRect (&limitRect,
  83.                 bounds.left   - dragBBox->left,
  84.                 bounds.top    - dragBBox->top,
  85.                 bounds.right  - dragBBox->right + 1,
  86.                 bounds.bottom - dragBBox->bottom + 1);
  87.     
  88.     OffsetRect (&limitRect, startPt.h, startPt.v);
  89.     slopRect = limitRect;
  90.     InsetRect (&slopRect, -slop, -slop);
  91.     
  92.     delta = DragGrayRgn (dragRgn, startPt, &limitRect,
  93.                             &slopRect, constraint, actionProc);
  94.     *deltaV = HiWord (delta);
  95.     *deltaH = LoWord (delta);
  96.  
  97.     if (( delta  == 0)
  98.     ||  (*deltaH == 0x8000)
  99.     ||  (*deltaV == 0x8000)) {
  100.         return (false);
  101.     } else {
  102.         return (true);
  103.     }
  104. } /*TrackMove*/
  105.  
  106. /*----------*/
  107. Boolean TrackRange (Point        anchorPt,
  108.                     Rect        *range)
  109. {
  110.     PenState        savePen;
  111.     Point            curPos;
  112.     Point            newPos;
  113.     long            lastTicks;
  114.  
  115.     GetPenState (&savePen);
  116.     PenNormal ();
  117.     PenMode (notPatXor);
  118. #ifdef dangerousPattern
  119.     PenPat (qd.gray);
  120. #else
  121.     PenPat (&qd.gray);
  122. #endif
  123.  
  124.     newPos = anchorPt;
  125.     curPos = newPos;
  126.     Pt2Rect (anchorPt, curPos, range);
  127.     FrameRect (range);    /*Draw*/
  128.  
  129.     lastTicks = 0;    
  130.     while (StillDown ()) {
  131.         if (TickCount () >= lastTicks + 2) {
  132.             GetMouse (&newPos);
  133.             if (!EqualPt (newPos, curPos)) {
  134.                 FrameRect (range);    /*Undraw*/
  135.                 curPos = newPos;
  136.                 Pt2Rect (anchorPt, curPos, range);
  137.                 FrameRect (range);    /*Draw*/
  138.             }
  139.             lastTicks = TickCount ();
  140.         }
  141.     } /*while*/
  142.     
  143.     FrameRect (range);    /*Undraw*/
  144.     
  145.     SetPenState (&savePen);
  146.  
  147.     return (!EmptyRect (range));
  148. } /*TrackRange*/
  149.  
  150. /*----------*/
  151. static void PinBotRight  (Rect        limitRect,
  152.                           Rect        *sizeRect);
  153. static void PinBotRight  (Rect        limitRect,
  154.                           Rect        *sizeRect)
  155. {
  156.     #define lR    limitRect
  157.     if (sizeRect->bottom < lR.top)        sizeRect->bottom = lR.top;
  158.     if (sizeRect->right < lR.left)        sizeRect->right = lR.left;
  159.     if (sizeRect->bottom > lR.bottom)    sizeRect->bottom = lR.bottom;
  160.     if (sizeRect->right > lR.right)        sizeRect->right = lR.right;
  161.     #undef lR
  162. } /*PinBotRight*/
  163.  
  164. /*----------*/
  165. static Boolean InSlop     (Rect    *sizeRect,
  166.                          Rect    *slopRect);
  167. static Boolean InSlop     (Rect    *sizeRect,
  168.                          Rect    *slopRect)
  169. {
  170.     Boolean        isIn;
  171.     Point        bR;
  172.     
  173.     bR.v = sizeRect->bottom;
  174.     bR.h = sizeRect->right;
  175.     isIn = PtInRect (bR, slopRect);
  176.     return (isIn);
  177. } /*InSlop*/
  178.  
  179. /*----------*/
  180. Boolean TrackRect  (Rect            *sizeRect,
  181.                     Rect            limitRect,
  182.                     DragProcPtr        actionProc)
  183. {
  184.     PenState        savePen;
  185.     Point            curPos;
  186.     Point            newPos;
  187.     Point            offset;
  188.     Point            bR;
  189.     Rect            slopRect;
  190.     long            lastTicks;
  191.     long            temp;
  192.  
  193.     GetPenState (&savePen);
  194.     PenNormal ();
  195.     PenMode (notPatXor);
  196. #ifdef dangerousPattern
  197.     PenPat (qd.gray);
  198. #else
  199.     PenPat (&qd.gray);
  200. #endif
  201.  
  202.     slopRect = limitRect;
  203.     InsetRect (&slopRect, -20, -20);
  204.     
  205.     GetMouse (&curPos);
  206.     bR.v = sizeRect->bottom;
  207.     bR.h = sizeRect->right;
  208.     temp = DeltaPoint (bR, curPos);
  209.     offset = *(Point *) &temp;
  210.  
  211.     FrameRect (sizeRect);    /*Draw*/
  212.  
  213.     lastTicks = 0;    
  214.     while (StillDown ()) {
  215.         if (TickCount () >= lastTicks + 2) {
  216.             GetMouse (&newPos);
  217.             if (!EqualPt (newPos, curPos)) {
  218.                 if (InSlop (sizeRect, &slopRect)) {
  219.                     FrameRect (sizeRect);    /*Undraw*/
  220.                 }
  221.                 curPos = newPos;
  222.                 AddPt (offset, &newPos);
  223.                 sizeRect->bottom = newPos.v;    /*botRight (sizeRect) = newPos;*/
  224.                 sizeRect->right = newPos.h;
  225.                 if (InSlop (sizeRect, &slopRect)) {
  226.                     PinBotRight (limitRect, sizeRect);
  227.                     FrameRect (sizeRect);    /*Draw*/
  228.                 }
  229.                 if (actionProc != NULL) {
  230.                     (*actionProc) ();
  231.                 }
  232.             }
  233.             lastTicks = TickCount ();
  234.         }
  235.     } /*while*/
  236.  
  237.     if (InSlop (sizeRect, &slopRect)) {
  238.         FrameRect (sizeRect);    /*Undraw*/
  239.     }
  240.     
  241.     SetPenState (&savePen);
  242.     
  243.     return (InSlop (sizeRect, &slopRect));
  244. } /*TrackRect*/
  245.  
  246. /*----------*/
  247. static void FrameLine (Rect        sizeRect);
  248. static void FrameLine (Rect        sizeRect)
  249. {
  250.     MoveTo (sizeRect.left, sizeRect.top);
  251.     LineTo (sizeRect.right - 1, sizeRect.bottom - 1);
  252. } /*FrameLine*/
  253.  
  254. /*----------*/
  255. Boolean TrackLine  (Rect            *sizeRect,
  256.                     Rect            limitRect,
  257.                     DragProcPtr        actionProc)
  258. {
  259.     PenState        savePen;
  260.     Point            curPos;
  261.     Point            newPos;
  262.     Point            Offset;
  263.     Point            bR;
  264.     Rect            slopRect;
  265.     long            lastTicks;
  266.     long            temp;
  267.  
  268.     GetPenState (&savePen);
  269.     PenNormal ();
  270.     PenMode (notPatXor);
  271. #ifdef dangerousPattern
  272.     PenPat (qd.gray);
  273. #else
  274.     PenPat (&qd.gray);
  275. #endif
  276.  
  277.     slopRect = limitRect;
  278.     InsetRect (&slopRect, -20, -20);
  279.     
  280.     GetMouse (&curPos);
  281.     bR.v = sizeRect->bottom;
  282.     bR.h = sizeRect->right;
  283.     temp = DeltaPoint (bR, curPos);
  284.     Offset = *(Point *) &temp;
  285.  
  286.     FrameLine (*sizeRect);    /*Draw*/
  287.  
  288.     lastTicks = 0;    
  289.     while (StillDown ()) {
  290.         if (TickCount () >= lastTicks + 2) {
  291.             GetMouse (&newPos);
  292.             if (!EqualPt (newPos, curPos)) {
  293.                 if (InSlop (sizeRect, &slopRect)) {
  294.                     FrameLine (*sizeRect);    /*Undraw*/
  295.                 }
  296.                 curPos = newPos;
  297.                 AddPt (Offset, &newPos);
  298.                 sizeRect->bottom = newPos.v;    /*botRight (sizeRect) = newPos;*/
  299.                 sizeRect->right = newPos.h;
  300.                 /* ?? constrain line to be horizontal or vertical ?? */
  301.                 /*    With sizeRect do { */
  302.                 /*        if ((right - left) > (bottom - top)) { */
  303.                 /*            bottom = top + 1; */
  304.                 /*        } else { */
  305.                 /*            right = left + 1; */
  306.                 /*        } */
  307.                 /*    } */ /*with*/
  308.                 if (InSlop (sizeRect, &slopRect)) {
  309.                     PinBotRight (limitRect, sizeRect); 
  310.                     FrameLine (*sizeRect);    /*Draw*/
  311.                 }
  312.                 if (actionProc != NULL) {
  313.                     (*actionProc) ();
  314.                 }
  315.             }
  316.             lastTicks = TickCount ();
  317.         }
  318.     } /*while*/
  319.  
  320.     if (InSlop (sizeRect, &slopRect)) {
  321.         FrameLine (*sizeRect);    /*Undraw*/
  322.     }
  323.     
  324.     SetPenState (&savePen);
  325.     
  326.     return (InSlop (sizeRect, &slopRect));
  327. } /*TrackLine*/
  328.